package ncse.chessgui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.*;

import ncse.checkerOP.*;

public class ChessUI extends JFrame implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private PicPanel p;
	private JMenuBar menuBar;
	private JMenu menu;
	private JMenuItem mItemPalyer;
	private JMenuItem mItemComputer;

	public ChessUI() {
		this.setSize(625, 655);
		this.setTitle("Tic Tac Toe");

		menu = new JMenu("Options");

		mItemPalyer = new JMenuItem("player first");
		mItemComputer = new JMenuItem("Computer first");
		mItemPalyer.addActionListener(this);
		mItemComputer.addActionListener(this);
		menu.add(mItemPalyer);
		menu.add(mItemComputer);

		menuBar = new JMenuBar();
		menuBar.add(menu);

		this.add(menuBar, BorderLayout.NORTH);

		p = new PicPanel();
		p.setLayout(new FlowLayout());
		this.add(p, BorderLayout.CENTER);

		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}

	public static void main(String[] args) {
		new ChessUI();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		Object eventSource = e.getSource();

		if (eventSource == mItemComputer) {
			p.computer_first();
		} else if (eventSource == mItemPalyer) {
			p.player_first();
		}
	}
}

class PicPanel extends JPanel implements MouseListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static int N = 4;
	private String bgpath = null;
	private String iconXpath = null;
	private String iconOpath = null;
	private ImageIcon iconX;
	private ImageIcon iconO;
	private ImageIcon playerIcon;
	private ImageIcon computerIcon;
	private int player = -1;
	private int computer = 1;
	private Checker checker;
	private boolean startflag = false;

	private int draw_position[][] = { { 50, 53 }, { 180, 53 }, { 310, 53 }, { 440, 53 }, 
			{ 50, 175 }, { 180, 180 }, { 310, 185 }, { 440, 180 },
			{ 55, 330 }, { 190, 325 }, { 320, 325 }, { 445, 320 }, 
			{ 65, 465 }, { 195, 460 }, { 325, 455 }, { 445, 450 } };
	
	private int tell_positionX[] = { 0, 177, 305, 435, 625 };
	private int tell_positionY[] = { 0, 187, 323, 470, 632 };

	public PicPanel() {
		bgpath = "./res/background.jpg";
		iconXpath = "./res/X.png";
		iconOpath = "./res/O.png";
		java.net.URL imgXURL = getClass().getResource(iconXpath);
		iconX = new ImageIcon(imgXURL);
		java.net.URL imgOURL = getClass().getResource(iconOpath);
		iconO = new ImageIcon(imgOURL);

		checker = new Checker();

		this.addMouseListener((MouseListener) this);
	}

	private int get_position(int[] positions, int pos) {
		int i = 0;
		for (i = 0; i < N; i++) {
			if (pos > positions[i] && pos < positions[i + 1]) {
				return i;
			}
		}
		return i - 1;
	}

	// 点击player first后调用的函数。
	// 输入：void
	// 返回值：void
	public void player_first() {
		checker.clear_board();
		startflag = true;
		playerIcon = iconX;
		computerIcon = iconO;
		checker.setWhofirst(this.player);
		this.repaint();
	}

	// 点击computer first后调用的函数。
	// 输入：void
	// 返回值：void
	public void computer_first() {
		checker.clear_board();
		startflag = true;
		playerIcon = iconO;
		computerIcon = iconX;
		checker.setWhofirst(this.computer);
		int step = checker.get_next_step();

		checker.place_piece(step, computer);
		this.repaint();
	}

	@Override
	public void paint(Graphics g) {
		int x = 0, y = 0;
		java.net.URL imgURL = getClass().getResource(bgpath);
		ImageIcon icon = new ImageIcon(imgURL);
		g.drawImage(icon.getImage(), x, y, 625, 600, this);
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				if (checker.get_chess_board_val(i, j) == 1) {
					g.drawImage(computerIcon.getImage(), draw_position[i * N
							+ j][0], draw_position[i * N + j][1], this);
				}
				if (checker.get_chess_board_val(i, j) == -1) {
					g.drawImage(playerIcon.getImage(),
							draw_position[i * N + j][0], draw_position[i * N
									+ j][1], this);
				}
			}
		}
	}

	private void print_message(String msg) {
		JOptionPane.showMessageDialog(this, msg, "结果",
				JOptionPane.INFORMATION_MESSAGE);
	}

	private void message_box_you_win() {
		this.print_message("You Win");
	}

	private void message_box_you_lose() {
		this.print_message("You Lose");
	}

	private void message_box_draw() {
		this.print_message("Game Draw");
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		int pos_x = 0, pos_y = 0;
		pos_x = get_position(tell_positionX, e.getX());
		pos_y = get_position(tell_positionY, e.getY());
		// 采用玩家-电脑放置棋子的更新方式。每次玩家下完棋后电脑立即下棋并更新界面
		// 如果电脑先手，可以在选择“computer first”后立即由电脑下棋，之后的步骤仍然是玩家-电脑下棋
		if (startflag && checker.get_chess_board_val(pos_y, pos_x) == 0) {
			checker.place_piece(pos_y*N+pos_x, player);
			this.repaint();
			if (checker.who_win() == -1) {
				startflag = false;
				this.message_box_you_win();
				return ;
			}
			if (checker.is_chess_board_full() && checker.who_win() == 0) {
				startflag = false;
				this.message_box_draw();
				return ;
			}

			int step = checker.get_next_step();

			if (checker.who_win() == 0 && checker.is_chess_board_full() != true) {
				checker.place_piece(step, computer);
				this.repaint();
			}
			if (checker.who_win() == 1) {
				startflag = false;
				this.message_box_you_lose();
				return ;
			}
			if (checker.is_chess_board_full() && checker.who_win() == 0) {
				startflag = false;
				this.message_box_draw();
				return ;
			}
		}
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}
}
